package com.yty.newconsult.task;

import cn.hutool.http.HttpUtil;
import com.yty.newconsult.entity.DailyNews;
import com.yty.newconsult.service.DailyNewsService;
import org.json.JSONArray;
import org.json.JSONObject;
import com.yty.newconsult.entity.News;
import com.yty.newconsult.service.InvokeApiService;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Component
public class DailyNewsTask {
    @Autowired
    private InvokeApiService invokeApiService;
    @Autowired
    private DailyNewsService dailyNewsService;
    private final String ts_key="487b4d5950ca01579e9127d289d45140";
    @Scheduled(cron = "0 0 17 * * ?") // 每天早上11点执行一次
    public void fetchAndStoreNews() throws ParseException {
        System.out.println("开始执行定时任务");
        int n=0;
        List<DailyNews> entities=new ArrayList<>();
        while(n<400){
            String ts_url = "https://api.tanshuapi.com/api/toutiao/v1/index";
            Map<String,Object> params=new HashMap<>();
            params.put("num",40);
            params.put("start",n);
            params.put("key",ts_key);

            // 1. 调接口获取新闻
            String res= HttpUtil.get(ts_url, params);
            JSONObject jsonObject = new JSONObject(res);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray newsList =data.getJSONArray("list");
            // 2. 清洗内容
            for (int i = 0; i < newsList.length(); i++) {
                JSONObject article = newsList.getJSONObject(i);
                String title = article.getString("title");
                String content = article.getString("content");
                LocalDate date =LocalDate.parse(article.getString("time"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                //清洗html标签
                content= Jsoup.parse(content).text();
                String url = article.getString("url");
                String pic=article.getString("pic");
                String weburl=article.getString("weburl");
                String category=article.getString("category");
                String src=article.getString("src");
                entities.add(new DailyNews(title,date,src,category,pic,url,weburl,content));
            }
            n+=40;
        }
        // 3. 插入数据库
        dailyNewsService.insertBatch(entities);
    }
}